;(function (doc) {
  const data = [
    "http://qiniuyun.quancundexiwang.wang/0.png",
    "http://qiniuyun.quancundexiwang.wang/01f9dd5ee82cd1a801206621efe81b.jpg@1280w_1l_2o_100sh.jpg",
    "http://qiniuyun.quancundexiwang.wang/0c8e100ba80f15ed321145a91d7afee8.gif",
    "http://qiniuyun.quancundexiwang.wang/1.jpg",
    "http://qiniuyun.quancundexiwang.wang/1.png",
    "http://qiniuyun.quancundexiwang.wang/1015416.jpg",
    "http://qiniuyun.quancundexiwang.wang/1028032.jpg",
    "http://qiniuyun.quancundexiwang.wang/1028034.jpg",
    "http://qiniuyun.quancundexiwang.wang/1039763.jpg",
    "http://qiniuyun.quancundexiwang.wang/1046781.jpg",
    "http://qiniuyun.quancundexiwang.wang/1047156.jpg",
    "http://qiniuyun.quancundexiwang.wang/1049734.jpg",
    "http://qiniuyun.quancundexiwang.wang/1055056.png",
    "http://qiniuyun.quancundexiwang.wang/1080487.png",
    "http://qiniuyun.quancundexiwang.wang/1081170.jpg",
    "http://qiniuyun.quancundexiwang.wang/1081215.jpg",
    "http://qiniuyun.quancundexiwang.wang/133753.jpg",
    "http://qiniuyun.quancundexiwang.wang/13949989-dcf024be2733e725.png",
    "http://qiniuyun.quancundexiwang.wang/1615818035394.png",
    "http://qiniuyun.quancundexiwang.wang/172cbb85e5310d93",
    "http://qiniuyun.quancundexiwang.wang/172cbb861dc31313",
    "http://qiniuyun.quancundexiwang.wang/1D6E7D39.png",
    "http://qiniuyun.quancundexiwang.wang/1阿斯顿.jpg",
    "http://qiniuyun.quancundexiwang.wang/2.jpg",
    "http://qiniuyun.quancundexiwang.wang/20200903001313.gif",
    "http://qiniuyun.quancundexiwang.wang/20200903101735.jpg",
    "http://qiniuyun.quancundexiwang.wang/20200903105754.png",
    "http://qiniuyun.quancundexiwang.wang/20200903105930.jpg",
    "http://qiniuyun.quancundexiwang.wang/20200903110331.png",
    "http://qiniuyun.quancundexiwang.wang/20200903114709.png",
    "http://qiniuyun.quancundexiwang.wang/20200903130423.png",
    "http://qiniuyun.quancundexiwang.wang/20200903142016.png",
    "http://qiniuyun.quancundexiwang.wang/20200903142102.png",
    "http://qiniuyun.quancundexiwang.wang/20200903143244.png",
    "http://qiniuyun.quancundexiwang.wang/20200903143440.png",
    "http://qiniuyun.quancundexiwang.wang/20200903145011.png",
    "http://qiniuyun.quancundexiwang.wang/20200903145523.png",
    "http://qiniuyun.quancundexiwang.wang/20200903163349.png",
    "http://qiniuyun.quancundexiwang.wang/20200903164147.png",
  ];
  const screenWidth = window.innerWidth;
  const screenHeight = window.innerHeight;

  function init() {
    const heightArr = [];
    let minHeightSpaceIndex;
    for (let i = 0; i < data.length; i++) {
      const waterFallWrapper = doc.getElementById("water-fall-wrapper");
      const itemWrapper = doc.createElement("div");
      const itemImg = doc.createElement("img");
      itemWrapper.setAttribute("class", "wf-item");
      itemWrapper.appendChild(itemImg);
      itemImg.setAttribute("srcData", data[i]);
      itemImg.src =
        "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3286238346,2636386852&fm=26&gp=0.jpg";
      waterFallWrapper.appendChild(itemWrapper);
      itemImg.style.width = (screenWidth - 40) / 5 + "px";
      itemImg.onload = function () {
        minHeightSpaceIndex = getMinNumberOfArray(heightArr);
        console.log(minHeightSpaceIndex);
        if (i < 5) {
          itemWrapper.style.top = 0;
          if ((i + 1) % 5 == 1) {
            itemWrapper.style.left = "0px";
          } else {
            itemWrapper.style.left = i * (itemImg.offsetWidth + 10) + "px";
          }
          heightArr.push(itemImg.offsetHeight);
        } else {
            itemWrapper.style.left = (itemImg.offsetWidth+10)*minHeightSpaceIndex+"px";
            itemWrapper.style.top = heightArr[minHeightSpaceIndex]+10+"px";
            heightArr[minHeightSpaceIndex]+=itemImg.offsetHeight;
        }
      };
    }
    renderImages();
  }

  function getMinNumberOfArray(arr) {
    const minItem = Math.min.call(this, ...arr);
    return Array.prototype.indexOf.call(arr, minItem);
  }

  function renderImages() {
    const itemImgs = document.querySelectorAll("img");
    itemImgs.forEach((item) => {
      if (item.getBoundingClientRect().top < screenHeight) {
        item.src = item.getAttribute("srcData");
      }
    });
  }

  window.onload = function () {
    init();
    window.addEventListener(
      "scroll",
      debounce(() => {
        renderImages();
      })
    );
  };
})(document);
